-
-
Notifications
You must be signed in to change notification settings - Fork 28
docs: TypeGPU functions #1793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: TypeGPU functions #1793
Conversation
|
pkg.pr.new packages benchmark commit |
reczkok
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually 10/10 docs - I would read them for fun even though I know all those things 🙇
|
|
||
| In order to construct a TypeGPU function, you need to start by defining its shell, an object holding only the input and output types. | ||
| :::note[WGSL enthusiasts!] | ||
| Don't let the JavaScript scare you! TypeGPU functions can be implemented using either WGSL or JS, both being able to call one another. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe discourage over scare? Scare sounds a little condescending to me.
| const main = () => { | ||
| 'use gpu'; | ||
| // Call from another function | ||
| return neighborhood(1.1, 0.5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those numbers are quite carefully chosen I see 😄 I think we should either wrap them in d.f32() or at least explain the dangers of using plain literals (eg: showing the 1.0 case)
EDIT: I just now reached a section where you explain it in depth - maybe we should do a short warning with a reference to that in case people get trigger happy and do not reach that?
| To make this all work, we perform a small transformation to functions marked with `'use gpu'`, leaving all other code untouched. Every project's setup is different, and we want to be as non-invasive as possible. The [unplugin-typegpu](/TypeGPU/tooling/unplugin-typegpu) package hooks into existing bundlers and build tools, extracts ASTs from TypeGPU functions and compacts them into our custom format called [tinyest](https://www.npmjs.com/package/tinyest). This metadata is injected into the final JS bundle, then used to efficiently generate equivalent WGSL at runtime. | ||
| :::tip | ||
| If you wish for all WGSL generation to happen at build time, combine [unplugin-macros](https://github.com/unplugin/unplugin-macros) and our [resolve API](/TypeGPU/fundamentals/resolve). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all WGSL generation at build time is not possible most of the time so we shouldn't sell it like that - maybe all build time known WGSL generation?
| * **Operators** -- | ||
| JavaScript does not support operator overloading. | ||
| This means that, while you can still use operators for numbers, | ||
| you have to use supplementary functions from `typegpu/std` (*add, mul, eq, lt, ge...*) for operations involving vectors and matrices, or use a fluent interface (*abc.mul(xyz), ...*) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could mention the //ts-ignore (no @ since of course there is a user with that username) trick for people who like cursed code 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't want people to abuse that and feel like we're endorsing it, as it introduces more problems down the line (result inferred as any, not working properly on the CPU)
| * **Math.\*** -- | ||
| Utility functions on the `Math` object can't automatically run on the GPU, but can usually be swapped with functions exported from `typegpu/std`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could mention that constants are fine
| "starlight-typedoc": "^0.19.0", | ||
| "tinybench": "^3.1.0", | ||
| "typedoc": "^0.28.13", | ||
| "typedoc-plugin-markdown": "^4.3.0", | ||
| "typedoc": "^0.27.9", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is, the newer versions of these packages didn't generate any API Reference docs, but they also didn't fail the nr build command.
aleksanderkatan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great docs!
I leave some nits
| TypeGPU functions let you define shader logic in a modular and type-safe way. | ||
| **TypeGPU functions** let you define shader logic in a modular and type-safe way. | ||
| Their signatures are fully visible to TypeScript, enabling tooling and static checks. | ||
| Dependencies, including GPU resources or other functions, are resolved automatically, with no duplication or name clashes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependencies are resolved automatically, but the externals are collected automatically only with unplugin & TGSL, do we want to mention it here?
| // ^? | ||
|
|
||
| // #2) Used to generate WGSL | ||
| const wgsl = tgpu.resolve({ externals: { main } }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be a little more explicit here, since this is close to the very beginning of the docs.
| const wgsl = tgpu.resolve({ externals: { main } }); | |
| const wgsl = tgpu.resolve({ template: "", externals: { main } }); |
I really think we should add a simplified resolve api btw
| ``` | ||
|
|
||
| TypeGPU then propagates those types into the function body and analyses the types returned by the function. | ||
| If it cannot unify them into a single type, it will throw an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
them may be ambiguous here
| If it cannot unify them into a single type, it will throw an error. | |
| If it cannot unify the types found in different return statements into a single type, it will throw an error. |
cieplypolar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful docs! Easy to read yet comprehensive 🦖
| ::: | ||
|
|
||
| * **Calling other functions** -- | ||
| Only functions marked with `'use gpu'` can be called from within a shader. An exception to that rule is [`console.log`](/TypeGPU/fundamentals/utils#consolelog), which allows for tracking runtime behavior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only functions marked with 'use gpu' - what about shelled functions? They don't have to be marked with 'use gpu', however we can call them from within a shader.
| return getGradientColor((in.uv[0] + in.uv[1]) / 2); | ||
| }`.$uses({ getGradientColor }); | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should include an example using the TypeGPU function in addition to raw WGSL. When using TypeGPU functions, we need to explicitly name the in parameter, for example: (input) => { ... }.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand what you mean 👀
|
|
||
| ## Storage and uniform variables | ||
|
|
||
| When using TypeGPU for resolution, there is no need to ever define either `var<uniform>` or `var<storage[, address_space]>`, as such definitions are added automatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| When using TypeGPU for resolution, there is no need to ever define either `var<uniform>` or `var<storage[, access_mode]>`, as such definitions are added automatically. |
| | `var<uniform>` | `root.createUniform()` | | ||
| | `var<storage, read>` | `root.createReadonly()` | | ||
| | `var<storage, read_write>` | `root.createMutable()` | | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a comment about as('...'), because we can make buffer a fixed resource.
No description provided.